package com.shux.redis.dao.impl;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import com.shux.redis.dao.IRedisDao;
import com.shux.utils.string.StringUtil;

/**
 **********************************************
 *  描述：
 * Simba.Hua
 * 2017年7月22日
 **********************************************
**/
@Repository("redisDao")
public class RedisDaoImpl<T> implements IRedisDao<T>{
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource(name = "redisClusterTemplate")
    private RedisTemplate<Object,T> redisClusterTemplate;
    
    /**
     * 新增数据
     * @param key
     * @param value
     */
    public void set(Object key,T value,Integer timeout){
        ValueOperations<Object, T> opsForValue = redisClusterTemplate.opsForValue();
        opsForValue.set(key, value, timeout, TimeUnit.SECONDS);
    }
    /**
     * 获取值
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public T get(Object key){
        ValueOperations<Object, T> opsForValue = redisClusterTemplate.opsForValue();
        return opsForValue.get(key);
    }
    /**
     * 删除为key的值
     * @param key
     */
    public void delete(String key){
        logger.info(StringUtil.join("delete the key ",key," from redis!"));
         redisClusterTemplate.delete(key);
    }
    /**
     * 
     * @param keys
     */
    public void deleteBatch(Collection<String> keys){
        redisClusterTemplate.delete(keys);
    }
    public RedisTemplate<Object, T> getRedisClusterTemplate() {
        return redisClusterTemplate;
    }
    
}
